layui.use(['table', 'admin', 'ax', 'func','laydate', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var func = layui.func;
    var laydate = layui.laydate;

    /**
     * 基本信息表管理
     */
    var TBasicinfo = {
        tableId: "tBasicinfoTable"
    };


    // 渲染时间选择框
    laydate.render({
        elem: '#startTime'
    });
    // 渲染时间选择框
    laydate.render({
        elem: '#endTime'
    });


    /**
     * 初始化表格的列
     */
    TBasicinfo.initColumn = function () {
        return [[
            {field: 'countyName', sort: true, title: '所属县区'},
            {field: 'companyName', sort: true, title: '企业名称'},
            {field: 'socialCreditCode', sort: true, title: '统一社会信用代码'},
            {field: 'companyAddress', sort: true, title: '企业生产经营地址'},
            {field: 'companyType', sort: true, title: '行业类别'},
            {field: 'ownedPark', sort: true, title: '所属园区'},
            {field: 'area', sort: true, title: '占地面积'},
            {field: 'longitudeLatitude', sort: true, title: '经度'},
            {field: 'idDiscontinued', sort: true, title: '是否停产',title: '是否停产',templet: function (d) {
                    if(d.idDiscontinued == 0){
                        return '未停产';
                    } else {
                        return  '停产'
                    }
                }},
            {field: 'legalRepresentative', sort: true, title: '法定代表人'},
            {field: 'eiaNumber', sort: true, title: '环评批复号'},
            {field: 'pollutionPermitNumber', sort: true, title: '排污许可证编号'},
            {field: 'companyPerson', sort: true, title: '企业联系人'},
            {field: 'linkType', sort: true, title: '联系方式'},
            {field: 'companyPeople', sort: true, title: '企业人数'},
            {field: 'isFocus', sort: true, title: '是否重点监管企业',templet: function (d) {
                    if(d.isFocus == 0){
                        return '是';
                    } else {
                        return  '否'
                    }
                }},
            {field: 'companyOrganization', sort: true, title: '企业组织架构',templet: function (d) {
                    return '<div onclick=showMedia("img","image","'+d.companyOrganization+'") ><img id="img" src="'+d.companyOrganization+'" alt="" width="100px" height="50px"></ a></div>';
                }},
            {field: 'latitude', sort: true, title: '纬度'},
            {field: 'pollutionPermitDate', sort: true, title: '排污许可证日期'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    TBasicinfo.search = function () {
        var queryData = {};
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        table.reload(TBasicinfo.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    TBasicinfo.openAddDlg = function () {
        func.open({
            title: '添加基本信息表',
            content: Feng.ctxPath + '/tBasicinfo/add',
            tableId: TBasicinfo.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    TBasicinfo.openEditDlg = function (data) {
        func.open({
            title: '修改基本信息表',
            content: Feng.ctxPath + '/tBasicinfo/edit?id=' + data.id,
            tableId: TBasicinfo.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    TBasicinfo.exportExcel = function () {
        var checkRows = table.checkStatus(TBasicinfo.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    TBasicinfo.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/tBasicinfo/delete", function (data) {
                if (data.code == 200) {
                    Feng.success("删除成功!");
                }else{
                    Feng.error("删除失败!" + data.message + "!");
                }
                table.reload(TBasicinfo.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TBasicinfo.tableId,
        url: Feng.ctxPath + '/tBasicinfo/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: TBasicinfo.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TBasicinfo.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        TBasicinfo.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        TBasicinfo.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + TBasicinfo.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            TBasicinfo.openEditDlg(data);
        } else if (layEvent === 'delete') {
            TBasicinfo.onDeleteItem(data);
        }
    });

    form.on('switch(delFlag)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

         TBasicinfo.changeDelFlag(id, checked);
    });

    /**
     * 修改状态
     *
     * @param userId 用户id
     * @param checked 是否选中（true,false），选中就是解锁用户，未选中就是锁定用户
     */
     TBasicinfo.changeDelFlag = function (id, checked) {
        if (checked) {
            var ajax = new $ax(Feng.ctxPath + "/tBasicinfo/enable", function (data) {
                if (data.code == 200) {
                    Feng.success("启用成功!");
                }else{
                    Feng.error("启用失败!" + data.message + "!");
                }
                table.reload( TBasicinfo.tableId);
            }, function (data) {
                Feng.error("启用失败!" + data.message + "!");
                table.reload( TBasicinfo.tableId);
            });
            ajax.set("id", id);
            ajax.start();
        } else {
            var ajax = new $ax(Feng.ctxPath + "/tBasicinfo/disable", function (data) {
                if(data.code == 200) {
                    Feng.success("禁用成功!");
                }else{
                    Feng.error("禁用失败!" + data.message + "!");
                }
                table.reload( TBasicinfo.tableId);
            }, function (data) {
                Feng.error("禁用失败!" + data.message + "!");
                table.reload( TBasicinfo.tableId);
            });
            ajax.set("id", id);
            ajax.start();
        }
    };
});


function showMedia(id, type, src) {
    var $ = layui.jquery;
    var idBar ="#"+id;
    var width = $(idBar).width();
    var height = $(idBar).height();
    var scaleWH = width / height;
    var bigH = 500;
    var bigW = scaleWH * bigH;
    if (bigW > 900) {
        bigW = 900;
        bigH = bigW / scaleWH;
    } // 放大预览图片
    layer.open({
        type: 1,
        title: false,
        closeBtn: 1,
        shadeClose: true,
        area: [bigW + 'px', bigH + 'px'], //宽高
        content: "<img width='" + bigW + "' height='" + bigH + "'controls=\"controls\" src=" + src + " />"
    });
}
